package com.eric.rabbit.config;

import com.eric.rabbit.entity.direct.DirectReceiver;
import com.eric.rabbit.entity.direct.DirectSender;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author:Eric
 * @date:2021/6/28 路由键（direct）模式
 * 一个生产者 -> 一个exchange -> 两个队列Queue -> 各自对应的消费者
 * exchange -> Queue1( red , yellow)
 * exchange -> Queue2( red , blue)
 * 各自消费各自的消息
 */
@Configuration
public class DirectRabbitConfig {

    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("direct.exchange");
    }

    @Bean
    public Queue directQueue1() {
        //代表一个匿名的、非持久的、独占的、自动删除的队列
        return new AnonymousQueue();
    }

    @Bean
    public Queue directQueue2() {
        //代表一个匿名的、非持久的、独占的、自动删除的队列
        return new AnonymousQueue();
    }

    @Bean
    public Binding directBind1a() {
        //进行exchange与queue绑定
        return BindingBuilder.bind(directQueue1()).to(directExchange()).with("red");
    }

    @Bean
    public Binding directBind1b() {
        //进行exchange与queue绑定
        return BindingBuilder.bind(directQueue1()).to(directExchange()).with("yellow");
    }

    @Bean
    public Binding directBind2a() {
        //进行exchange与queue绑定
        return BindingBuilder.bind(directQueue2()).to(directExchange()).with("red");
    }

    @Bean
    public Binding directBind2b() {
        //进行exchange与queue绑定
        return BindingBuilder.bind(directQueue2()).to(directExchange()).with("blue");
    }

    @Bean
    public DirectSender directSender() {
        return new DirectSender();
    }

    @Bean
    public DirectReceiver directReceiver() {
        return new DirectReceiver();
    }

}
